package com.atguigu.e_reflect.constructor;

import java.lang.reflect.Constructor;

public class Demo {
    public static void main(String[] args) throws NoSuchMethodException {
        //反射第一步 : 获取类的字节码对象
        Class<Student> clazz = Student.class;

        //反射第二步 : 获取类中的成员[构造方法]
        Constructor<?>[] constructors = clazz.getDeclaredConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println("constructor = " + constructor);
        }

        System.out.println("---------------------------");
        //如何获取指定的构造方法对象呢 ? --> 根据形参类型的字节码对象来区分不同构造方法
        //无参构造的对象获取
        Constructor<Student> constructor1 = clazz.getDeclaredConstructor();
        //有参构造的对象获取 -> String name
        Constructor<Student> constructor2 = clazz.getDeclaredConstructor(String.class);
        //有参构造的对象获取 -> int age
        Constructor<Student> constructor3 = clazz.getDeclaredConstructor(int.class);
        //有参构造的对象获取 -> String name , int age
        Constructor<Student> constructor4 = clazz.getDeclaredConstructor(String.class, int.class);

        System.out.println("constructor1 = " + constructor1);
        System.out.println("constructor2 = " + constructor2);
        System.out.println("constructor3 = " + constructor3);
        System.out.println("constructor4 = " + constructor4);
    }
}
